package com.das.mechanic_base.adapter.processnew;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.def.alone.AloneUploadStatus;
import com.das.mechanic_base.utils.X3GlideRoundTransform;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.das.mechanic_base.widget.X3PointDialog;
import com.kproduce.roundcorners.RoundImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShowImageSiveAdapter extends RecyclerView.Adapter<ShowImageSiveAdapter.ShowImageHolder> {

    private List<AloneImageAndRecordBean> imageList;

    private Context mContext;

    private RequestOptions myOptions;

    private final X3PointDialog delDialog;

    private int titleIndex;

    public ShowImageSiveAdapter(List<AloneImageAndRecordBean> imageList, Context mContext, int titleIndex) {
        this.imageList = imageList;
        this.titleIndex = titleIndex;
        if (this.imageList == null) {
            this.imageList = new ArrayList<>();
        }
        this.mContext = mContext;
        myOptions = new RequestOptions()
                .centerCrop()
                .error(R.mipmap.x3_car_img_bg)
                .placeholder(R.mipmap.x3_car_img_bg)
                .transform(new X3GlideRoundTransform(mContext, 7));
        delDialog = new X3PointDialog((Activity) mContext, "", mContext.getString(R.string.x3_affirm_delete_image), mContext.getString(R.string.x3_cancel_info), mContext.getString(R.string.x3_affirm_info));

    }

    @NonNull
    @Override
    public ShowImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ShowImageHolder(LayoutInflater.from(mContext).inflate(R.layout.show_image_sive_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ShowImageHolder holder, final int position) {

        // 最后一项显示拍照
        if (position == imageList.size()) {
            // 隐藏删除按钮
            holder.image_delete.setVisibility(View.GONE);
            holder.iv_images.setVisibility(View.VISIBLE);
            holder.iv_image.setVisibility(View.GONE);
            holder.iv_images.setImageResource(R.mipmap.x3_camera);
            // 点击事件，最多上传9张照片
            holder.iv_images.setOnClickListener(v -> {
                if (position > 8) {
//                    ToastUtils.showMessage(mContext.getString(R.string.upload_mast_nine));
                    X3ToastUtils.showMessage(String.format(mContext.getString(R.string.x3_upload_mast_nine),9));
                    return;
                } else {
                    if (iOnClickTake != null) {
                        iOnClickTake.iOnClickTakeListener(position);
                    }
                }
            });

        } else {
            //holder.iv_image.setImageAlpha(255);
            holder.iv_image.setVisibility(View.VISIBLE);
            holder.iv_images.setVisibility(View.INVISIBLE);
            String imagePath = imageList.get(position).getPath();

            /**
             * 上传状态
             */
            long uploadStatus = imageList.get(position).getUploadStatus();

            switch ((int) uploadStatus) {
                case 0:
                    holder.image_delete.setVisibility(View.GONE);
                    holder.iv_bg.setVisibility(View.VISIBLE);
                    holder.pb_bar.setVisibility(View.VISIBLE);
                    holder.iv_failed.setVisibility(View.GONE);
                    holder.iv_bg.setOnClickListener(null);
                    break;
                case 1:
                    holder.image_delete.setVisibility(View.VISIBLE);
                    holder.iv_bg.setVisibility(View.GONE);
                    holder.pb_bar.setVisibility(View.GONE);
                    holder.iv_failed.setVisibility(View.GONE);
                    holder.iv_bg.setOnClickListener(null);
                    break;
                case 2:
                    holder.image_delete.setVisibility(View.VISIBLE);
                    holder.iv_bg.setVisibility(View.VISIBLE);
                    holder.pb_bar.setVisibility(View.GONE);
                    holder.iv_failed.setVisibility(View.VISIBLE);
                    holder.iv_bg.setOnClickListener(v -> {
                        // 重新上传图片
                        // 重新上传图片
                        String httpPath = imageList.get(position).getHttpPath();
                        if (!X3StringUtils.isEmpty(httpPath)) {
                            // 网络图片不为空 无需上传
                            imageList.get(position).setUploadStatus(AloneUploadStatus.UPLOADING_SUCCESS);
                            notifyItemChanged(position);
                            return;
                        }
                        String path = imageList.get(position).getPath();
                        if (X3StringUtils.isEmpty(path)) {
                            // 本地图片为空 无需上传 并且需要删除
                            /**
                             * 并且在数据库中要删除
                             */
                            if (iOnClickTake != null) {
                                iOnClickTake.iOnDeleteImage(titleIndex,imageList.get(position).getPath());
                            }
                            imageList.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position,imageList.size() - position);
                            return;
                        }
                        File file = new File(path);
                        if (!file.exists()) {
                            // 本地无法找到此图片 同样要从列表中移除
                            /**
                             * 并且在数据库中要删除
                             */
                            if (iOnClickTake != null) {
                                iOnClickTake.iOnDeleteImage(titleIndex,imageList.get(position).getPath());
                            }
                            imageList.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position,imageList.size() - position);
                            return;
                        }

                        imageList.get(position).setUploadStatus(AloneUploadStatus.UPLODING);
                        notifyItemChanged(position);
                        if (iOnClickTake != null) {
                            iOnClickTake.iOnAgainUploadImage(imageList.get(position).getPath(), titleIndex, position);
                        }
                    });
                    break;
            }

            if (!X3StringUtils.isEmpty(imagePath)) {
                holder.iv_play.setVisibility(imagePath.endsWith(".mp4") ? View.VISIBLE : View.GONE);

                if (!imagePath.contains("storage")) {
                    imagePath = "http://" + imagePath;
                }
                //GlideNewUtils.loadRoundCornerImage(mContext,imagePath,holder.iv_image, ScreenUtils.dipToPx(4,mContext),R.mipmap.car_img_bg);
                Glide.with(mContext).load(imagePath).apply(new RequestOptions().centerCrop().error(R.mipmap.x3_car_img_bg).placeholder(R.mipmap.x3_car_img_bg)).into(holder.iv_image);
            }

            holder.image_delete.setOnClickListener(v -> {

                if (delDialog != null) {
                    delDialog.show();
                }
                delDialog.setiBtnClick(new X3PointDialog.IBtnClick() {
                    @Override
                    public void iBtnAffirmClick() {
                        long id = imageList.get(position).getId();
                        if (id == 0) {
                            /**
                             * 并且在数据库中要删除
                             */
                            if (iOnClickTake != null) {
                                iOnClickTake.iOnDeleteImage(titleIndex,imageList.get(position).getPath());
                            }
                            imageList.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position,imageList.size() - position);
                            return;
                        }
                        imageList.get(position).setUploadStatus(AloneUploadStatus.UPLODING);
                        notifyItemChanged(position);
                        if (iOnClickTake != null) {
                            iOnClickTake.iOnClickRemoveListener(position);
                        }
                    }

                    @Override
                    public void iBtnCancelClick() {
                    }
                });

            });

            holder.iv_image.setOnClickListener(v -> {
                List<String> imgList= new ArrayList<>();
                for (AloneImageAndRecordBean imageAndRecordBean : imageList) {
                    imgList.add(imageAndRecordBean.getPath());
                }
                X3Utils.startShowImage(mContext,imgList,position);
            });

        }
    }

    IOnClickTake iOnClickTake;

    public void setiOnClickTake(IOnClickTake iOnClickTake) {
        this.iOnClickTake = iOnClickTake;
    }

    public interface IOnClickTake {

        void iOnClickTakeListener(int position);

        void iOnClickRemoveListener(int imgIndex);

        /**
         * 点击重新上传图片
         *
         * @param path
         * @param titleIndex
         * @param imageIndex
         */
        void iOnAgainUploadImage(String path, int titleIndex, int imageIndex);

        /**
         * 之前没有上传成功 则直接删除
         * @param titleIndex
         * @param imagePath
         */
        void iOnDeleteImage(int titleIndex,String imagePath);
    }

    @Override
    public int getItemCount() {

        return X3StringUtils.isListEmpty(imageList) ? 1 : imageList.size() + 1;
    }

    class ShowImageHolder extends RecyclerView.ViewHolder {
        RoundImageView iv_image;
        ImageView image_delete;
        ImageView iv_images;
        ImageView iv_play;
        ImageView iv_failed;
        ImageView iv_bg;
        ProgressBar pb_bar;

        public ShowImageHolder(View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
            iv_images = itemView.findViewById(R.id.iv_images);
            image_delete = itemView.findViewById(R.id.image_delete);
            iv_play = itemView.findViewById(R.id.iv_play);
            iv_failed = itemView.findViewById(R.id.iv_failed);
            iv_bg = itemView.findViewById(R.id.iv_bg);
            pb_bar = itemView.findViewById(R.id.pb_bar);
        }
    }
}
